package com.cs.beneficios.web.action;
import com.cs.beneficios.business.model.ProgramaVO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.business.entity.ProgramaDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class OtorgarBeneficiosAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    String strCode = request.getParameter("code");
    String strUsuario = request.getParameter("Usuario");
    
    Object codePrograma =  (Object)request.getParameter("codePrograma");
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO)session.getAttribute("userInfo");

    session.setAttribute("beneficiario",strCode); //OJO ESTA LINEA ES LA QUE HAY QUE DEJAR
    //session.setAttribute("beneficiario","46"); // LINEA DE PRUEBA
    
    session.setAttribute("Usuario",user.getCode().toString());
    //session.setAttribute("Usuario","51"); // LINEA DE PRUEBA
    
    Collection programas=null;
    Collection subprogramas=null;
    ProgramaDAO objProgramaDAO;
    ProgramaVO objProgramaVO =  new ProgramaVO();
    SubProgramaDAO objSubProgramaDAO;
    SubProgramaVO objSubProgramaVO =  new SubProgramaVO();
    
    if(codePrograma == null){ 
      session.removeAttribute("programas");
      try
      {
        objProgramaDAO = new ProgramaDAO(cf);
        programas = objProgramaDAO.findProgramaByCritereaOB(objProgramaVO,user.getCode(),user.getUserGroupName(),user.getCodeArea());
        cf.closeConnection(objProgramaDAO.getConn());
      }catch(SQLException e){
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSUB - cwsub ********/");
      }
      session.removeAttribute("msg");
      if(programas.size() > 0){
        
        session.setAttribute("programas",programas);
      }else
      {
        session.setAttribute("msg","No se encontraron programas vigentes a la fecha..");
      }
    }
    
    if(codePrograma != null)
    {
      session.removeAttribute("subprogramas");
      String codpro = (String)codePrograma;
      if(!codpro.equals("nulo"))
      {
        try
        {
          objSubProgramaVO.setCodePrograma(new Long((String)codePrograma));
          objSubProgramaDAO = new SubProgramaDAO(cf);
          subprogramas = objSubProgramaDAO.findSubProgramaByCriterea(objSubProgramaVO,user.getCode(),user.getUserGroupName(),user.getCodeArea());
          cf.closeConnection(objSubProgramaDAO.getConn());
        }catch(SQLException e){
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
        }
        session.removeAttribute("msg");
        if(subprogramas.size() > 0){
          session.setAttribute("subprogramas",subprogramas);
        }else
        {
          session.setAttribute("msg","No se encontraron subprogramas asociados a este programa que esten vigentes..");
        }
      }else
          session.setAttribute("msg","");
    }
    
    return mapping.findForward("success");
  }
}